﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GameBase;
using Microsoft.Xna.Framework.Graphics;
using System.Diagnostics;

namespace GalaxyPresident
{
    public class DetailPlanetView : VisibleGroup
    {
        //public int NPLANET = 10;
        List<CraftStation> craftStations = new List<CraftStation>();
        List<AutoAttack> autoAttackEntity = new List<AutoAttack>();

        internal List<AutoAttack> AutoAttackEntity
        {
            get { return autoAttackEntity; }
            set { autoAttackEntity = value; }
        }

        internal List<CraftStation> CraftStations
        {
            get { return craftStations; }
            set { craftStations = value; }
        }
        static Texture2D TEXTURE =
            Constant.content.Load<Texture2D>(@"Resources/moon");
        BBB planet;

        public override void AddChild(VisibleGameEntity child)
        {
            base.AddChild(child);
            if (child is CraftStation)
            {
                craftStations.Add((CraftStation)child);
            }
            else if (child is AutoAttack)
            {
                autoAttackEntity.Add((AutoAttack)child);
            }
        }
        public override void RemoveChild(VisibleGameEntity child)
        {
            base.RemoveChild(child);
            if (child is CraftStation)
            {
                craftStations.Remove((CraftStation)child);
            }
            else if (child is AutoAttack)
            {
                autoAttackEntity.Remove((AutoAttack)child);
            }
        }
        public override void RemoveAllChild()
        {
            base.RemoveAllChild();
            craftStations.Clear();
            autoAttackEntity.Clear();
        }
        public BBB Planet
        {
            get { return planet; }
            set { planet = value; }
        }
        public bool isOrigin=false;
        public DetailPlanetView(Planet planet)
        {
            isOrigin = true;
            this.planet = planet;
            MutiSpriteObject obj = new MutiSpriteObject();
            Sprite ocean = new Sprite(@"Resources/Planet/p", 1, 1);
            ocean.SetOriginPosition(ORIGIN_POSITION.Center);
            ocean.Color = planet.BackColor;
            int id = planet.PatternId;
            Sprite land = new Sprite(@"Resources/Planet/p"+
                id.ToString("000"), 1, 1);
            land.SetOriginPosition(ORIGIN_POSITION.Center);
            land.Color = planet.LandColor;
            land.Rotate = (float)(Constant.random.NextDouble() * Math.PI * 2);
            obj.AddSprite(ocean);
            obj.AddSprite(land);
            obj.Scale = Constant.scaleDetailPlanet * planet.Diameter / ocean.Width;
            obj.Rotate = planet.PatternRotate;
            obj.Depth = 0.99f;
            this.AddChild(obj);
            foreach (Block block in planet.Blocks)
            {
                if (block.Entity != null)
                {
                    AddChild(block.Entity);
                }
            }

            
            //circleSprite.Color = 
        }
        public DetailPlanetView(Moon moon)
        {

            this.planet = moon;
            VisibleObject obj = new VisibleObject(@"Resources/moon", 1, 1);
            obj.Sprite.SetOriginPosition(ORIGIN_POSITION.Center);
            obj.Scale = Constant.scaleDetailPlanet * planet.Diameter / obj.Sprite.Width;
            obj.Depth = 0.99f;
            obj.Rotate = (float)(Constant.random.NextDouble() * Math.PI * 2);
            this.AddChild(obj);
            foreach (Block block in planet.Blocks)
            {
                if (block.Entity != null)
                {
                    AddChild(block.Entity);
                }
            }
            Position = Constant.scaleDetailPlanet * planet.Position;
        }

        public override void Update(Microsoft.Xna.Framework.GameTime gameTime)
        {
            if (Planet.GetType() == typeof(LivalePlanet))
            {
                Debug.WriteLine(@"Environment: " + ((LivableBlock)(this.Planet.Blocks[0])).Environment.ToString());
                Debug.WriteLine(@"Population: " + ((LivableBlock)(this.Planet.Blocks[0])).Population.ToString());
                Debug.WriteLine(@"Economic: " + ((LivableBlock)(this.Planet.Blocks[0])).Economic.ToString());
                Debug.WriteLine(@"Happiness: " + ((LivableBlock)(this.Planet.Blocks[0])).Happiness.ToString());
            }

            base.Update(gameTime);
            if (isOrigin)
            {
                
                return;
            }
            Position = Constant.scaleDistanceDetailPlanet * Planet.Position;
        }
        public override void UpdateInfo(Microsoft.Xna.Framework.GameTime gameTime)
        {
            base.UpdateInfo(gameTime);
            for (int i = 0; i < autoAttackEntity.Count; i++)
            {
                AutoAttack source = autoAttackEntity[i];
                for (int j = 0; j < autoAttackEntity.Count; j++)
                {
                    AutoAttack target = autoAttackEntity[j];
                    if (i != j && source.GetTeamOwner() != target.GetTeamOwner() &&
                        (((VisibleGameEntity)target).Position -
                        ((VisibleGameEntity)source).Position).Length() < source.GetRange()*5)
                    {
                        source.attackTo((PlanetObjectOwner)target);
                    }
                }
               /* foreach (CraftStation station in craftStations)
                {
                    if (source.GetTeamOwner() != station.GetTeamOwner() &&
                       station.IsAlert(((VisibleGameEntity)source).Position))
                    {
                        station.Attack(((VisibleGameEntity)source).Position);
                    }
                }*/
            }
        }

    }
}
